package cn.fl.dk.web.service.calculator.impl;

import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.dk.facade.constant.DkQuestneLogConstant;
import cn.fl.dk.web.service.calculator.ScoreCalculator;
import cn.fl.dk.web.vo.CalculateResult;
import com.fasterxml.jackson.databind.ser.Serializers;
import fl.score.mgt.facade.score.score.api.ISysScoreIdxOptQueryApiService;
import fl.score.mgt.facade.score.score.api.ISysScoreIdxQueryApiService;
import fl.score.mgt.facade.score.score.dto.SysScoreIdxDTO;
import fl.score.mgt.facade.score.score.dto.SysScoreIdxOptDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * 未出险问卷分数计算方式
 * 作者:hongyanbo
 * 时间:2018/6/8
 */
@Service("examBScoreCalculator")
public class ExamBScoreCalculator implements ScoreCalculator{
    private Logger LOG= LoggerFactory.getLogger(ExamBScoreCalculator.class);
    @Autowired
    private ISysScoreIdxQueryApiService idxQueryApiService;
    @Autowired
    private ISysScoreIdxOptQueryApiService optQueryApiService;

    private HashMap<Integer,Integer> scoreMap ;
    @Override
    public CalculateResult cal(List<KeyValueVO> list) {
        CalculateResult result = new CalculateResult();
        scoreMap = new HashMap<>();
        int i = 1;
        for (KeyValueVO vo : list) {
            long idxId = Long.parseLong(vo.getKey());
            long optId = Long.parseLong(vo.getValue());
            SysScoreIdxDTO idxDTO = idxQueryApiService.selectSysScoreIdxDTOById(idxId);
            SysScoreIdxOptDTO optDTO = optQueryApiService.selectSysScoreIdxOptDTOById(optId);
            if(optDTO != null){
                Long scrVal = optDTO.getScrVal();
                String optTypCd = optDTO.getOptTypCd();
                Long sortNo = idxDTO.getSortNo();
                if(optTypCd!=null){
                    if(optTypCd.equals("1")){ //单选 要求必须有分值
                        if(scrVal==null){
                            BaseException baseException = new BaseException("模板选项scrVal为空，请检查数据模板选项数据正确性optId:" + optId);
                            LOG.error("分数计算错误",baseException);
                            throw baseException;
                        }
                        if(sortNo==null){
                            BaseException baseException = new BaseException("模板题号sortNo为空，请检查模板题目数据正确性idxId:" + idxId);
                            LOG.error("分数计算错误",baseException);
                            throw baseException;
                        }
                        scoreMap.put(sortNo.intValue(),scrVal.intValue());
                    }
                }
            }

            i++ ;
        }
        int m = 0;
        int n = 0;
        try {
            // m=s1*s2*s3
            if(scoreMap.containsKey(1) && scoreMap.containsKey(2) && scoreMap.containsKey(3)){
                m = scoreMap.get(1) * scoreMap.get(2) * scoreMap.get(3);
            }
            // n=（S7+S8-S9）*S10*S11；
            n = (scoreMap.get(8)+ scoreMap.get(9) -scoreMap.get(10)) * scoreMap.get(11) * scoreMap.get(12) ;
            if(m==1){ //预期拖回
                result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_3));
            }else if(m ==0 && n>=2){ //逾期还款
                result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_4));
            }else if(m==0 && n<2){ // 预期逾期追加
                result.setScore(Long.valueOf(DkQuestneLogConstant.QTN_RST_CD_2));
            }
            LOG.info("问卷分数计算结果为{}",result.getScore());
            // 判断是否相符 ,不相符的话 取更严重的结果
            int sc = scoreMap.get(12);
            Long score = result.getScore();
            if(sc==0){ //选项预期追加
                if(score.intValue()!=2){ //算分结果是非预期还款，最终结果按照计算结果，设置不相符
                    result.setScore(2l);
                    result.setIsMatch("0");
                }else{
                    result.setIsMatch("1");
                }


            }else{ //预期还款
                if(score.intValue()!=4){
                    result.setIsMatch("0"); //不相符
                }else{
                    result.setIsMatch("1");
                }
            }
        }catch (Exception e){
            BaseException baseException = new BaseException("未出险问卷分数计算失败,未获取到某题的分数");
            LOG.error("分数计算错误",baseException);
            throw baseException;
        }
        LOG.info("问卷分数最终结果为:{}",result.getScore());
        return result;
    }
}
